package com.suntop.carousel.qlmediaplay.fragement;

import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.suntop.carousel.qlmediaplay.bean.PlayerState;
import com.suntop.carousel.qlmediaplay.field.Field_Banner_Player;
import com.suntop.carousel.qlmediaplay.media.IMediaPlayEventListener;
import com.suntop.carousel.qlmediaplay.tools.Rxbus;

import java.io.Serializable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import io.reactivex.functions.Consumer;

public abstract class BasicFragment<T> extends Fragment implements IMediaPlayEventListener, Consumer<PlayerState> {
    protected static final String ARG_Position = "Position";
    protected static final String ARG_T = "Serializable";
    private T t = null;
    protected int position = 0;
    private IMediaPlayEventListener iMediaPlayEventListener;
    private FrameLayout frameLayout;
    private boolean isAutoPlay = false;
    private int program_list_id;
    private int program_id;
    private String tag;


    protected static Bundle newBundleInstance(int position, Object t) {
        Bundle args = new Bundle();
        args.putInt(ARG_Position, position);

        if (t instanceof Serializable) {
            args.putSerializable(ARG_T, (Serializable) t);
        }
        if (t instanceof Parcelable) {
            args.putParcelable(ARG_T, (Parcelable) t);
        }
        if (t instanceof String) {
            args.putString(ARG_T, (String) t);
        }

        return args;
    }

    /**
     * 可以自动播放
     *
     * @return
     */
    public boolean isAutoPlay() {
        return isAutoPlay;
    }

    public void setProgram_list_id(int program_list_id) {
        this.program_list_id = program_list_id;
    }

    public void setProgram_id(int program_id) {
        this.program_id = program_id;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        if (getArguments() != null) {
            position = getArguments().getInt(ARG_Position);

            Object o = getArguments().get(ARG_T);
            if (o != null) {
                t = (T) o;
            }
        }
        super.onCreate(savedInstanceState);
    }

    public void setIMediaPlayEventListener(IMediaPlayEventListener listener) {
        this.iMediaPlayEventListener = listener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (frameLayout != null) {
            frameLayout.removeAllViews();
            frameLayout = null;
        }
        frameLayout = new FrameLayout(inflater.getContext());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.CENTER;
        frameLayout.setLayoutParams(layoutParams);
        if (getContext() == null) {
            return frameLayout;
        }
        initView(frameLayout, t);
        return frameLayout;
    }

    /**
     * 初始化数据
     *
     * @param frameLayout
     * @param t
     */
    protected abstract void initView(FrameLayout frameLayout, T t);


    public void registerRxbus() {
        tag = System.currentTimeMillis() + "" + program_list_id + "" + program_id + "" + position;
        Log.e("注册 片段",tag+"   program_id="+program_id+"  position="+position);
        Rxbus.getInStance().registerRxbus(Field_Banner_Player.Fragment_Player, tag, this);
    }


    @Override
    public void startMediaPlay(int position, int type, String url) {
        if (iMediaPlayEventListener != null) {
            iMediaPlayEventListener.startMediaPlay(position, type, url);
        }
    }

    @Override
    public void cacheMediaPlay(int position, int type, String url, String path) {
        if (iMediaPlayEventListener != null) {
            iMediaPlayEventListener.cacheMediaPlay(position, type, url, path);
        }
    }

    @Override
    public void finishMediaPlay(int position, int type, String url, String error) {
        if (iMediaPlayEventListener != null) {
            iMediaPlayEventListener.finishMediaPlay(position, type, url, error);
        }
    }

    public abstract void startPlay();

    @Override
    public void onDestroy() {
        if (frameLayout != null) {
            frameLayout.removeAllViews();
        }
        unRegisterRxBus(tag);
        super.onDestroy();
    }

    public void unRegisterRxBus(String tag) {
        Rxbus.getInStance().unRegisterRxBus(Field_Banner_Player.Fragment_Player, tag);
    }

    @Override
    public void accept(PlayerState playerState) throws Exception {
        if (playerState == null) {
            return;
        }
        int state = playerState.getState();
        int program_list_id = playerState.getProgram_list_id();
        int program_id = playerState.getProgram_id();
        int position = playerState.getPosition();

        if (this.program_list_id != program_list_id) {
            return;
        }
        if (program_id != this.program_id) {
            return;
        }
        Log.e("接受指令 播放界面",
                "program_id="+ program_id+" 当前的节目="+this.program_id+
                        " 播放 ="+state+" 当前="+this.position +" 接受="+position);
        switch (state) {
            case PlayerState.State.Start:
                if (!isAutoPlay) {
                    isAutoPlay = true;
                }
                if (this.position==position&&isResumed()&&isVisible()){
                    startPlay();
                }
                break;
            case PlayerState.State.Pause:
                if (this.position==position&&isResumed()){
                    onPause();
                }
                break;
            case PlayerState.State.Stop:
            case PlayerState.State.Rest:
                if (this.position==position)
                onStop();
                break;
            case PlayerState.State.Release:
                onDestroy();
                break;
        }

    }
}
